﻿/*
 * Rappelz: Endless Odyssey - The first free open source Rappelz server emulator
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rappelz.GameServer.Network;
using System.Data.Common;
using System.Data;
using Rappelz.GameServer.Network.GameClient;

namespace Rappelz.GameServer.Database
{
    /// <summary>
    /// DB_UpdateSummon (smp_read_character_list, smp_read_item_equip_info)
    /// </summary>
    public class DB_UpdateSummon : IDBProc
    {
        int IN_SID;
        int IN_ACCOUNT_ID;
        int IN_OWNDER_ID;
        int IN_CODE;
        string IN_NAME;
        int IN_LV;
        int IN_JLV;
        int IN_MAX_LEVEL;
        long IN_EXP;
        long IN_JP;
        long IN_LAST_DECREASED_EXP;
        int IN_HP;
        int IN_MP;
        int IN_SP;
        int IN_FP;
        int IN_PREV_LEVEL_01;
        int IN_PREV_LEVEL_02;
        int IN_PREV_ID_01;
        int IN_PREV_ID_02;
        int IN_TRANSFORM;

        private bool m_bSaveAll;

        public BaseObject m_CBObject = null;

        public DB_UpdateSummon(Summon summon, bool bSaveAll)
        {
            IN_SID = summon.m_nSID;
            IN_ACCOUNT_ID = summon.m_master != null ? summon.m_master.m_nAccountID : 0;
            IN_OWNDER_ID = summon.m_master != null ? summon.m_master.m_nUID : 0;
            IN_CODE = summon.m_ContentInfo.uid;
            IN_NAME = summon.m_szName;
            IN_LV = summon.m_nLevel;
            IN_JLV = summon.m_nJobLevel;
            IN_MAX_LEVEL = summon.m_nMaxReachedLevel;
            IN_EXP = summon.m_nEXP;
            IN_JP = summon.m_nJobPoint;
            IN_LAST_DECREASED_EXP = summon.m_nLastDecreasedEXP;
            IN_HP = summon.m_nHP;
            IN_MP = (int)summon.m_fMP;
            IN_SP = summon.m_nSP;
            IN_FP = 0;
            IN_PREV_LEVEL_01 = summon.m_nPrevJobLevel[0];
            IN_PREV_LEVEL_02 = summon.m_nPrevJobLevel[1];
            IN_PREV_ID_01 = summon.m_nPrevJobId[0];
            IN_PREV_ID_02 = summon.m_nPrevJobId[1];
            IN_TRANSFORM = summon.m_nTransformLevel;

            m_bSaveAll = bSaveAll;
        }

        public void SetCB(BaseObject obj)
        {
            m_CBObject = obj;
        }

        public void RunCB()
        {
            if (m_CBObject != null)
                m_CBObject.onEndQuery();
        }

        public bool onProcess()
        {
            DbConnection conn = GameDBManager.Instance.CreateConnection();
            DbCommand cmd = conn.CreateCommand();

            cmd.CommandText = "smp_update_summon";
            cmd.CommandType = CommandType.StoredProcedure;

            GameDBManager.Instance.CreateInParameter(cmd, "IN_SID", DbType.Int32, IN_SID);
            GameDBManager.Instance.CreateInParameter(cmd, "IN_ACCOUNT_ID", DbType.Int32, IN_ACCOUNT_ID);
            GameDBManager.Instance.CreateInParameter(cmd, "IN_OWNDER_ID", DbType.Int32, IN_OWNDER_ID);
            GameDBManager.Instance.CreateInParameter(cmd, "IN_CODE", DbType.Int32, IN_CODE);
            GameDBManager.Instance.CreateInParameter(cmd, "IN_NAME", DbType.String, IN_NAME);
            GameDBManager.Instance.CreateInParameter(cmd, "IN_LV", DbType.Int32, IN_LV);
            GameDBManager.Instance.CreateInParameter(cmd, "IN_JLV", DbType.Int32, IN_JLV);
            GameDBManager.Instance.CreateInParameter(cmd, "IN_MAX_LEVEL", DbType.Int32, IN_MAX_LEVEL);
            GameDBManager.Instance.CreateInParameter(cmd, "IN_EXP", DbType.Int64, IN_EXP);
            GameDBManager.Instance.CreateInParameter(cmd, "IN_JP", DbType.Int32, IN_JP);
            GameDBManager.Instance.CreateInParameter(cmd, "IN_LAST_DECREASED_EXP", DbType.Int64, IN_LAST_DECREASED_EXP);
            GameDBManager.Instance.CreateInParameter(cmd, "IN_HP", DbType.Int32, IN_HP);
            GameDBManager.Instance.CreateInParameter(cmd, "IN_MP", DbType.Int32, IN_MP);
            GameDBManager.Instance.CreateInParameter(cmd, "IN_SP", DbType.Int32, IN_SP);
            GameDBManager.Instance.CreateInParameter(cmd, "IN_FP", DbType.Int32, IN_FP);
            GameDBManager.Instance.CreateInParameter(cmd, "IN_PREV_LEVEL_01", DbType.Int32, IN_PREV_LEVEL_01);
            GameDBManager.Instance.CreateInParameter(cmd, "IN_PREV_LEVEL_02", DbType.Int32, IN_PREV_LEVEL_02);
            GameDBManager.Instance.CreateInParameter(cmd, "IN_PREV_ID_01", DbType.Int32, IN_PREV_ID_01);
            GameDBManager.Instance.CreateInParameter(cmd, "IN_PREV_ID_02", DbType.Int32, IN_PREV_ID_02);
            GameDBManager.Instance.CreateInParameter(cmd, "IN_TRANSFORM", DbType.Int32, IN_TRANSFORM);

            conn.Open();
            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Globals.Log.Error("DB_UpdateQuest.onProcess Exception: {0}", ex);
            }
            conn.Close();

            if(m_bSaveAll)
            {
                //updateStateInfo()
            }
            return true;
        }
    }

}
